package com.feng.test;

public class Cross {

	public static void main(String[] args) {
		final int NUM = 10;
		// 主程序
		System.out.println("开始穿过山洞了......");
		for (int i = 0; i < NUM; i++) {
			Thread thread = new Thread(new crossThread(i));
			thread.start();
			try {
				//join()的作用是：“等待该线程终止”;子线程调用了join()方法，（主线程）后面的代码只有等到子线程结束了才能执行。
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("全部穿过山洞了呀呀呀呀呀呀......");//加入了join方法后，该句最后输出
		
	}
}

class crossThread implements Runnable {
	static Object hole = new Object();
	int i = 0;

	public crossThread(int i) {
		this.i = i;
	}

	@Override
	public void run() {// 线程
		synchronized (hole) {//同步锁
			try {
				Thread.sleep(5000);// 5秒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("5秒后......");
			System.err.println("谁过来了？......");
			System.err.print(new Thread().getName());
			System.err.println("穿过山洞了啊啊啊啊啊啊啊");
		}

	}

}
